<?php

/**
 * Description of MailSystem : customzie the Swift_Message class to render view inside the body of the message
 *
 * @author Samaa Mohamed
 */
class MailSystem extends Swift_Message {

    /**
     *
     * @param sfController $controller
     * @param type $name
     * @param array $params
     * @param type $contentType
     * @param type $charset
     * @return MailerTemplate 
     * 
     */
    public function setBodyFromTemplate(sfController $controller, $name, array $params = array(), $contentType = null, $charset = null) {
        $view = $this->getView($controller,$name,$params);
        $this->setBody($view->render(), $contentType, $charset);
        return $this;
    }

    /**
     *
     * @param sfController $controller
     * @param string $name template name
     * @param array $params array of view params
     * @return sfView 
     */
    protected function getView(sfController $controller, $name, array $params) {
        $view = $controller->getView(null, $name, '');
        $view->setDirectory(sfConfig::get('sf_lib_dir').'/'.sfConfig::get('app_email_template_dir'));
        $view->execute();
        $view->setDecoratorTemplate(sfConfig::get('sf_lib_dir').'/'.sfConfig::get('app_email_layout'));
        $view->getAttributeHolder()->add($params);
        return $view;
    }

    /**
     * Create a new Message.
     * @param string $subject
     * @param string $body
     * @param string $contentType
     * @param string $charset
     * @return MailSystem
     */
    public static function newInstance($subject = null, $body = null, $contentType = null, $charset = null) {
        $object =  new self($subject, $body, $contentType, $charset);
        $object->setSender(array("nobody@max.com" => 'max'));
        $object->setFrom(array("nobody@max.com"=> 'max'));
        $object->setContentType("text/html");
        return $object;
    }

}

